
#include "gpio.h"

//============================================================================================================
 int gpio_export(int pin)
{
    int fd =  open("/sys/class/gpio/export", O_WRONLY);
    if(fd < 0)
    {
        return -1;
    }
    char pin_str[10] = {0};
    snprintf(pin_str, sizeof(pin_str), "%d", pin);
    if(write(fd, pin_str, strlen(pin_str)) < 0)
    {
        close(fd);
        printf("GPIO_INFO::Failed to gpio_export of gpio_%d !\n",pin);
        return(-1);
    }
    close(fd);
    return 0;
}

//============================================================================================================
int gpio_unexport(int pin)
{
    int fd =  open("/sys/class/gpio/unexport", O_WRONLY);
    if(fd < 0)
    {
        printf("fail to open /sys/class/gpio/export ---\n");
        return(-1);
    }
    char pin_str[10] = {0};
    snprintf(pin_str, sizeof(pin_str), "%d", pin);
    if(write(fd, pin_str, strlen(pin_str)) < 0)
    {
        printf("GPIO_INFO::Failed to unexport of gpio_%d ! \n", pin);
        close(fd);
        return(-1);
    }
    close(fd);
    return 0;
}

//============================================================================================================
//dir: 0-->IN, 1-->OUT
int gpio_direction(int pin, int dir)
{
    static const char *dir_str[] = {"in","out"};
    char path[64];
    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
    int fd = open(path, O_WRONLY);
    if (fd < 0)
    {
        printf("GPIO_INFO::Failed to set gpio direction of gpio_%d !\n",pin);
        return -1;
    }
    write(fd, *(dir_str+dir), strlen( *(dir_str+dir)) );
    close(fd);
    return 0;
}

//============================================================================================================
//value: 0-->LOW, 1-->HIGH
int gpio_write(int pin, int value)
{
    static const char values_str[] = "01";
    char path[64];
    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
    int fd = open(path, O_WRONLY);
    if (fd < 0)
    {
        printf("GPIO_INFO::Failed to open gpio_%d for writing !\n",pin);
        return -1;
    }
    if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
    {
        printf("GPIO_INFO::Failed to write value to gpio_%d !\n",pin);
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}
//============================================================================================================
int gpio_read(int pin)
{
    char path[64];
    char value_str[3];
    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
    int fd = open(path, O_RDONLY);
    if (fd < 0)
    {
        printf("GPIO_INFO::Failed to open gpio_%d for reading!,%s\n",pin,path);
        return -1;
    }
    if (read(fd, value_str, 3) < 0)
    {
        printf("Failed to read value!\n");
        close(fd);
        return -1;
    }
    close(fd);
    return (value_str[0] - 48);
}
//============================================================================================================
// none         表示引脚为输入，不是中断引脚
// rising       表示引脚为中断输入，上升沿触发
// falling      表示引脚为中断输入，下降沿触发
// both         表示引脚为中断输入，边沿触发
// 0-->none, 1-->rising, 2-->falling, 3-->both
//============================================================================================================
int gpio_edge(int pin, int edge)
{
    const char *dir_str[] = {"none", "rising", "falling", "both"};
    char ptr;
    char path[64];
    int fd;
    switch(edge){
        case 0:
            ptr = 0;
            break;
        case 1:
            ptr = 1;
            break;
        case 2:
            ptr = 2;
            break;
        case 3:
            ptr = 3;
            break;
        default:
            ptr = 2;
    }
    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
    fd = open(path, O_WRONLY);
    if(fd < 0)
    {
        printf("GPIO_INFO::Failed to open gpio_%d for writing interrupt mode!\n",pin);
        return -1;
    }
   write(fd, *(dir_str+ptr), strlen(*(dir_str+ptr)));
   close(fd);
   return 0;
}




